#!/usr/local/BLBIN/bin/php
<?php
function get_request($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	curl_close($ch);
	return trim($response);
}

function real_execute($cmd)
{
	$a = popen($cmd, 'r');

	while ($b = fgets($a, 2048)) {
		echo $b;
		ob_flush();
		flush();
	}

	pclose($a);
}

function install_software()
{
	global $plast_bin;
	global $api_license;
	echo "\x1b" . '[32m Installation will take about 10 minutes  ' . "\x1b" . '[0m ' . "\n";
	echo "\n";
	echo "\n";
	echo "\x1b" . '[32m Please Wait... ' . "\x1b" . '[0m ' . "\n";
	firewall_accept(true);
	echo 'Installing cln...';
	system('cd /home; wget https://repo.cloudlinux.com/cloudlinux/sources/cln/cldeploy; sh cldeploy -i');
	exec('sed -i -e \'s/enabled=0/enabled=1/g\' /etc/yum.repos.d/cloudlinux.repo');
	system('yum install lvemanager -y');
	$output_license = get_request((string) $api_license);
	$output_license = json_decode($output_license);
	$proxy_conf = $output_license->proxy_conf;
	$license_key = $output_license->key;
	$time = time();
	$file_conf = $time . '.conf';
	$path_conf = '/usr/bin/.log';
	$full_path = $path_conf . '/' . $file_conf;
	system('mkdir -p \'' . $path_conf . '\' &> /dev/null');
	file_put_contents($full_path, $proxy_conf);
	exec_license($full_path, $license_key);
	system('cd /home; sh cldeploy --skip-registration -k 99999');
	system('yum install cagefs lvemanager -y');
	system('yum groupinstall alt-php -y');
	system('yum reinstall lvemanager -y');
	echo "\n\n" . ' ' . "\x1b" . '[32m  CloudLinux has been installed. Please reboot to apply kernel updates. ' . "\x1b" . '[0m ' . "\n\n";
	exit();
}

function exec_output($cmd)
{
	exec($cmd, $output, $return_var);
	return $output[0];
}

function get_http_response_code($domain1)
{
	$ch = curl_init($domain1);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	$output = curl_exec($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $httpcode;
}

function firewall_accept($force = false)
{
	if (file_exists('/usr/sbin/csf')) {
		if (file_exists('/etc/csf/csf.conf')) {
			csf_ports();
		}
	}
}

function firewall_drop()
{
	global $firewall_stop;
	global $firewall_stop_1;

	if (file_exists('/usr/sbin/csf')) {
		if ($firewall_stop_1) {
			system('service csf -e &> /dev/null');
			system('rm -rf /etc/csf/csf.error &> /dev/null');
		}

		if ($firewall_stop) {
			system('service csf start &> /dev/null');
			system('rm -rf /etc/csf/csf.error &> /dev/null');
		}
	}
}

function csf_ports()
{
	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'TCP_OUT = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/TCP_OUT =/c\\TCP_OUT = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'TCP_IN = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/TCP_IN =/c\\TCP_IN = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'UDP_IN = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/UDP_IN =/c\\UDP_IN = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'TESTING = "0"');

	if ($pos === false) {
		exec('sed -i \'/TESTING =/c\\TESTING = "0"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'UDP_OUT = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/UDP_OUT =/c\\UDP_OUT = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}
}

function exec_license($file, $key)
{
	global $status;
	global $plast_bin;
	exec('chattr -i /var/lve/lveinfo.ver > /dev/null 2>&1; rm -rf /var/lve/lveinfo.ver > /dev/null');
	firewall_accept(true);
	system('chmod +x /usr/sbin/rhnreg_ks &> /dev/null');
	$output_check_license = exec_output(' ' . $plast_bin . ' -q -f \'' . $file . '\' /usr/bin/cldetect --update-new-key \'' . $key . '\' ');

	if (checkLicense()) {
		$status = true;
	}
	else {
		$status = false;
	}

	if ($status) {
		exec('sed -i -e \'s/enabled=0/enabled=1/g\' /etc/yum.repos.d/cloudlinux.repo');
		system('yum reinstall htop -y &> /dev/null');
	}

	system('rm -rf \'' . $file . '\' &> /dev/null');
	return $status;
}

function checkLicense()
{
	global $status;
	system('chmod +x /usr/bin/cldetect &> /dev/null');
	$output_check_license = exec_output(' /usr/bin/cldetect --check-license ');

	if (preg_match('/OK/', $output_check_license)) {
		$status = true;
	}
	else {
		$status = false;
	}

	return $status;
}

$RED = '\\033[31m';
$Green = '\\033[32m';
$Cyan = '\\033[36m';
$NC = '\\033[0m';
$key = 'cloudlinux';
$api = 'https://api.begpl.com/api/getinfo?key=' . $key;
$api_license = 'https://api.begpl.com/api/license?key=' . $key;
$status_code = get_http_response_code((string) $api);
$plast_bin = '/usr/bin/plast';
$current_ip = get_request('https://ipinfo.io/ip');
$domain_show = 'https://begpl.com';
$brand_show = 'begpl.com';
$hostname_show = exec_output('hostname');
$status = false;
$server_range = 0;
$key_cmd = 'gb';
$firewall_stop = false;
$firewall_stop_1 = false;
$force = false;
$installed = false;
$action = (1 < count($argv) ? $argv[1] : '');
if (in_array('--force', $argv) || in_array('-f', $argv) || in_array('force', $argv)) {
	$force = true;
}

echo "\n";
echo "\x1b" . '[32mPlease Wait important packages need to be installed ... ' . "\x1b" . '[0m ' . "\n";

if ($action != 'install') {
	if (!file_exists('/usr/sbin/clnreg_ks') || !file_exists('/usr/bin/cldetect')) {
		echo "\x1b" . '[31mCloudlinux is not detected ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[31mYou need to install Cloudlinux ' . "\x1b" . '[0m ' . "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[32mFor quick installation ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[32m/usr/bin/installer_run install  ' . "\x1b" . '[0m ' . "\n";
		echo "\n";
		exit();
	}
	else {
		$installed = true;
	}
}

if (!file_exists('/etc/redhat-release')) {
	system('yum install deltarpm  -y  1> /dev/null');
}

if (!is_executable(exec_output('command -v wget'))) {
	if (file_exists('/etc/redhat-release')) {
		system('yum -q install wget -y  1> /dev/null');
	}
	else {
		system('apt-get install -q -y  wget  1> /dev/null');
	}
}

$output = get_request($api);

if ($status_code != '200') {
	printf("\x1b" . '[31m Something Went Wrong [Unknown Ip]   ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	exit();
}

$output = json_decode($output, true);
$expire_date = $output['expire_date'];
$get_domain_show = $output['domain_name'];
$get_brand_show = $output['brand_name'];
$get_key_cmd_show = $output['key_cmd'];

if ($get_key_cmd_show != '') {
	$key_cmd = $get_key_cmd_show;
}

if ($get_domain_show != '') {
	$domain_show = $get_domain_show;
}

if ($get_brand_show != '') {
	$brand_show = $get_brand_show;
}

echo "\n";
echo "\n";
printf("\x1b" . '[36m---------------------- BeGPL Licensing System Started ----------------------  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Thank you for using our Cloudlinux Licensing System  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Our Website: ' . $domain_show . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Server IPV4: ' . $current_ip . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Hostname: ' . $hostname_show . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m| Expiry Date: ' . $expire_date . '  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36m----------------------------------------------------------------------  ' . "\x1b" . '[0m ' . "\n");
echo "\n";
echo "\n";
echo "\n";
printf("\x1b" . '[36mIf you have any question contact us on our website.  ' . "\x1b" . '[0m ' . "\n");
printf("\x1b" . '[36mCopyright © 2019-2021 ' . $brand_show . ' . All rights reserved ' . "\x1b" . '[0m ' . "\n");
echo "\n";
echo "\n";
printf("\x1b" . '[32mPlease Wait... ' . "\x1b" . '[0m ' . "\n");
if (!file_exists('/usr/bin/plast') || $force) {
	$cmd = '    wget -q   "http://api.begpl.com/api/files/plast/plast.tar.gz?key=' . $key . '"  -O "plast.tar.gz" &> /dev/null' . "\r\n" . '    tar xf plast.tar.gz';
	system($cmd);

	if (file_exists('/etc/redhat-release')) {
		$cmd = '    yum -q install git make gcc -y &> /dev/null' . "\r\n" . '    wget -q  -O-  "http://api.begpl.com/api/files/plast/installer.sh?key=' . $key . '" |  bash -s &> /dev/null' . "\r\n" . '    mv /usr/local/bin/proxychains4 /usr/bin/plast &> /dev/null' . "\r\n";
		system($cmd);
	}
	else if (file_exists('/etc/lsb-release') || file_exists('/etc/os-release')) {
		$cmd = '    apt install -y git make gcc -y &> /dev/null' . "\r\n" . '    wget -q  -O-  "http://api.begpl.com/api/files/plast/installer.sh?key=' . $key . '" |  bash -s &> /dev/null' . "\r\n" . '    mv /usr/local/bin/proxychains4 /usr/bin/plast &> /dev/null' . "\r\n";
		system($cmd);
	}
	else {
		printf("\x1b" . '[31m  Unsupported System  ' . "\x1b" . '[0m ' . "\n");
		exit();
	}
}

system('rm -rf  /etc/cron.d/licensecx  &> /dev/null');
system('wget -q  -O \'/usr/bin/' . $key_cmd . 'licensecx\'  \'http://api.begpl.com/api/files/' . $key . '/gblicensecx?key=' . $key . '\'  &> /dev/null');
system('wget -q  -O \'/usr/bin/' . $key_cmd . 'licensecx_update\'  \'http://api.begpl.com/api/files/' . $key . '/gblicensecx_update?key=' . $key . '\'  &> /dev/null');
system('chmod +x  \'/usr/bin/' . $key_cmd . 'licensecx_update\'  &> /dev/null');
system('chmod +x  \'/usr/bin/' . $key_cmd . 'licensecx\'  &> /dev/null');
echo "\n";
echo "\n";
echo "\n";
echo "\n";

if ($action == 'install') {
	if (!file_exists('/usr/bin/cldetect')) {
		install_software();
		echo "\n";
		echo "\n";

		if (!file_exists('/usr/bin/cldetect')) {
			printf("\x1b" . '[31mInstallation Status FAILED ' . "\x1b" . '[0m ' . "\n");
			$installed = false;
			$status = false;
		}
		else {
			printf("\x1b" . '[32mInstallation Status OK ' . "\x1b" . '[0m ' . "\n");
			$installed = true;
			$status = true;
		}
	}

	echo "\n";
	echo "\n";
}

firewall_accept();
$status = false;
if (!$status || ($action == '--force-update')) {
	$output_license = get_request((string) $api_license);
	$output_license = json_decode($output_license);
	$proxy_conf = $output_license->proxy_conf;
	$license_key = $output_license->key;
	$time = time();
	$file_conf = $time . '.conf';
	$path_conf = '/usr/bin/.log';
	$full_path = $path_conf . '/' . $file_conf;
	system('mkdir -p \'' . $path_conf . '\' &> /dev/null');
	file_put_contents($full_path, $proxy_conf);
	exec_license($full_path, $license_key);
	$status = checkLicense();
}

if (!$status) {
	printf("\x1b" . '[32mMethod 1 FAILED ' . "\x1b" . '[0m ' . "\n");

	while (!$status) {
		++$server_range;
		$extra_range = $server_range + 1;
		$output_license = get_request($api_license . '&server_range=' . $server_range);
		$output_license = json_decode($output_license);
		$proxy_conf = $output_license->proxy_conf;
		$license_key = $output_license->key;

		if ($proxy_conf != '') {
			$time = time();
			$file_conf = $time . '.conf';
			$path_conf = '/usr/bin/.log';
			$full_path = $path_conf . '/' . $file_conf;
			system('mkdir -p \'' . $path_conf . '\' &> /dev/null');
			file_put_contents($full_path, $proxy_conf);
			exec_license($full_path, $license_key);
			$status = checkLicense();

			if ((string) $status) {
				printf("\x1b" . '[32mMethod ' . $extra_range . ' OK ' . "\x1b" . '[0m ' . "\n");
				break;
			}
		}
		else {
			$status = false;
			break;
		}

		printf("\x1b" . '[32mMethod ' . $extra_range . ' FAILED ' . "\x1b" . '[0m ' . "\n");
	}
}

if ($status) {
	printf("\x1b" . '[32mYour licenses was successfully updated or renewed ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	echo "\n";
	printf("\x1b" . '[32mTo Re-New your Cloudlinux License you can use : ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[32m    ' . $key_cmd . 'licensecx  ' . "\x1b" . '[0m ' . "\n");
}
else {
	printf("\x1b" . '[32mCloudlinux Status FAILED ' . "\x1b" . '[0m ' . "\n");
}

echo "\n";
echo "\n";
firewall_drop();
$cronjob = 'PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin' . "\r\n\r\n" . '*/2 * * * * root /usr/bin/' . $key_cmd . 'licensecx \'only-check\' >/dev/null 2>&1' . "\r\n";
system('printf \'' . $cronjob . '\' > /etc/cron.d/licensecx');
system('sed -i -e "s/\\r//g" /etc/cron.d/licensecx');

?>